package main;

import java.sql.Date;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import system.Book;
import system.BookProxy;
import system.Order;
import system.OrderProxy;
import system.OrderState;
import system.User;
import system.UserIdentity;
import system.UserProxy;
import db.DBConnection;

public class DBUtil implements Service {
	private BookProxy mBookProxy;
	private OrderProxy mOrderProxy;
	private UserProxy mUserProxy;
	private DBConnection mDBConnection;
	
	public DBUtil(BookProxy pBookProxy, OrderProxy pOrderProxy, UserProxy pUserProxy, DBConnection pConnection) {
		mBookProxy = pBookProxy;
		mOrderProxy = pOrderProxy;
		mUserProxy = pUserProxy;
		mDBConnection = pConnection;
	}

	@Override
	public User insertUser(String pAccount, String pPassword, UserIdentity pIdentity, String pName, String pPhone, String pAddress) throws SQLException {
		User u = new User(pAccount, pPassword, pIdentity, pName, pPhone, pAddress);
		
		return mUserProxy.insert(u, mDBConnection);
	}

	@Override
	public int drop(User pUser) throws SQLException {
		return mUserProxy.drop(pUser, mDBConnection);
	}

	@Override
	public int update(User pUser) throws SQLException {
		return mUserProxy.update(pUser, mDBConnection);
	}

	@Override
	public List<User> queryUser(String pAccount, String pName, UserIdentity pIdentity, float pCreditPoint) throws SQLException {
		StringBuffer condition = new StringBuffer();
		if (pAccount != null) {
			condition.append("account = '" + pAccount +"' AND ");
		}
		
		if (pName != null) {
			condition.append("name='" + pName + "' AND ");
		}
		
		if (pIdentity != null) {
			condition.append("user_identity='" + pIdentity + "' AND ");
		}
		
		if (pCreditPoint >= 0) {
			condition.append("pCreditPoint>=" + pCreditPoint + "' AND ");
		}
		
		if (condition.length() > 5) {
			condition.delete(condition.length() - 5, condition.length());
		}
		
		return mUserProxy.query(mDBConnection, condition.toString());
	}

	@Override
	public Book insertBook(String pName, String pISBN, String pAuthor,
			String pPress, String pDescription, String pCatagory,
			Date pPublishDate, float pPrice, int pCount) throws SQLException {
		Book book = new Book(pName, pISBN, pAuthor, pPress, pDescription, pCatagory, pPublishDate, pPrice, pCount);
		
		return mBookProxy.insert(book, mDBConnection);
	}

	@Override
	public int drop(Book pBook) throws SQLException {
		return mBookProxy.drop(pBook, mDBConnection);
	}

	@Override
	public int update(Book pBook) throws SQLException {
		return mBookProxy.update(pBook, mDBConnection);
	}

	@Override
	public List<Book> queryBook(String pKeywordsOfBookName, String pAuthor) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Book> queryBoughtBooks(User pUser) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Order insertOrder(User pUser, Date pDate, HashMap<Book, Integer> bookAndCount, String pAddress, int pDiscountCoupons, int pEqualCoupns) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Order pOrder) throws SQLException {
		return 0;
	}

	@Override
	public int drop(Order pOrder) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Order> queryOrders(long pUserID, Date pDate, OrderState pState) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
